package com.sinosoft.system.util.generate;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class GenMainMysql {
	
	private static String entityPack = "com.sinosoft.app.entrust.entity";
	private static String daoPack = "com.sinosoft.app.entrust.dao";
	private static String daoImpPack = "com.sinosoft.app.entrust.dao.imp";
	private static String bizPack = "com.sinosoft.app.entrust.biz";
	private static String bizImpPack = "com.sinosoft.app.entrust.biz.imp";
	private static String actionPack = "com.sinosoft.app.entrust.action";
	private static String tableName = "entrust_order";
	private static String author ="shangxp";
	
	private  static String ftlPath = "D://git//morningglory//src//main//resources//ftl";
	private static String projectPath = "D://git//morningglory//src//main//java";
		
	private  String driver = "com.mysql.jdbc.Driver";
    private  String url = "jdbc:mysql://192.168.1.10:3306/appdb";
    private  String username = "root";
    private  String password = "root";
    
    
    /**
     * 数据库表转换为实体类map
     * @return
     */
    public Map<String,Object> doGenEntity() {
    	Connection con = null;
    	PreparedStatement ps = null;
    	ResultSet rs = null; 
    	Map<String,Object> dataMap = new HashMap<String,Object>();
    	List<Column> columns = new ArrayList<Column>();
    	try {
    		//加载驱动类
    		Class.forName(this.driver);
    		con = DriverManager.getConnection(this.url, this.username,this.password);
    		ps = con.prepareStatement("select table_name,column_name,column_comment,"
        		+ " column_type,column_key,column_default "
        		+ " from INFORMATION_SCHEMA.Columns "
        		+ "  where table_name='"+tableName+"' "
        		+ "  and table_schema='appdb'");
            rs = ps.executeQuery();
            while (rs.next()) {
            	Column col = new Column();
                String columnName = rs.getString("column_name");
                if(!"is_del".equalsIgnoreCase(columnName)){
                	col.setName(columnName);
                	col.setJname(GenUtil.initStr(columnName));
                	String type = rs.getString("column_type");
                	col.setType(type);
                	col.setJtype(GenUtil.sqlType2JavaType(type));
                	col.setComment(rs.getString("column_comment"));
                	columns.add(col);
                }
            }
            dataMap.put("entityPack", entityPack);
            dataMap.put("daoPack", daoPack);
            dataMap.put("daoImpPack", daoImpPack);
            dataMap.put("bizPack", bizPack);
            dataMap.put("bizImpPack", bizImpPack);
            dataMap.put("actionPack", actionPack);
            dataMap.put("author", author);
            dataMap.put("tableName", tableName);
            dataMap.put("className", GenUtil.upcaseName(tableName));
            dataMap.put("instName", GenUtil.initStr(tableName));
            dataMap.put("columns",columns);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
        	try {
				rs.close();
				ps.close();
	        	con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}        	
        }
    	return dataMap;
    }
    
    public static void main(String[] args){
    	GenMainMysql mainMysql = new GenMainMysql();
    	Map<String,Object> root = mainMysql.doGenEntity();
    	GenEntityMysqlFtl entityFtl = new GenEntityMysqlFtl();
    	entityFtl.doGenEntity(root, ftlPath, projectPath, entityPack);
    	String className = GenUtil.upcaseName(tableName);
    	GenMysqlFtl genJava = new GenMysqlFtl();
    	genJava.doGetJava(root, "entity.ftl", ftlPath, projectPath, entityPack,className);
    	genJava.doGetJava(root, "dao.ftl", ftlPath, projectPath, daoPack,className+"Dao");
    	genJava.doGetJava(root, "daoImp.ftl", ftlPath, projectPath, daoImpPack,className+"DaoJdbc");
    	genJava.doGetJava(root, "biz.ftl", ftlPath, projectPath, bizPack,className+"Biz");
    	genJava.doGetJava(root, "bizImp.ftl", ftlPath, projectPath, bizImpPack,className+"BizImp");
    	genJava.doGetJava(root, "action.ftl", ftlPath, projectPath, actionPack,className+"Action");
    	
    }
}
